package mysql

import (
	"gorm.io/gorm"
	"wechat/wechatrpc/wechatrpc"
)

type Moments struct {
	gorm.Model
	Image   string `gorm:"type:varchar(500)"`
	Article string `gorm:"type:varchar(300)"`
	UserId  int64  `gorm:"type:int(11)"`
}

func (m *Moments) MomentAdd(db *gorm.DB) error {
	return db.Debug().Create(m).Error
}

func (m *Moments) MomentShow(db *gorm.DB, id int64) error {
	return db.Debug().Where("id = ?", id).First(m).Error
}

func (m *Moments) MomentDel(db *gorm.DB, id int64) error {
	return db.Debug().Where("id = ?", id).Delete(m).Error
}

func MomentsList(db *gorm.DB, moment []*wechatrpc.MomentList) []*wechatrpc.MomentList {
	db.Debug().Model(&Moments{}).
		Select("moments.*,users.nick_name").
		Joins("left join users on users.id = moments.user_id").Find(&moment)
	return moment
}
